package servlets.customer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.Cart;
import services.ServiceCart;
import services.ServiceCheckCredit;

public class ServletBuyProducts extends HttpServlet {

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServiceCart serviceCart = new ServiceCart();
        ServiceCheckCredit serviceCheckCredit = new ServiceCheckCredit();
        HttpSession session = request.getSession();
        String userIdsession = (String) session.getAttribute("userId");
        int userId = Integer.parseInt(userIdsession);
        System.out.println("user id to check out ==================== " + userId);
        String checkCredit = serviceCheckCredit.checkCredit(userId);
        if (checkCredit.equals("buy")) {
            List<Cart> test = serviceCart.buyProductsInCart(userId);
            if (test != null) {
                if (test.size() <= 0) {
                    //save to history and reset cart//////////////////////////////////////
                    response.getWriter().print("<h4>Checked out successfully</h4>");
                } else //some products are out of stock
                {
                    response.getWriter().print("<h4>Sorry some products are out of stock</h4>");
                }
            } else {
                response.getWriter().print("<h4>Sorry your cart is empty</h4>");
            }
        } else // not enough credit
        {
            response.getWriter().print("<h4>Sorry you don't have enough credit</h4>");
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServiceCart serviceCart = new ServiceCart();
        ServiceCheckCredit serviceCheckCredit = new ServiceCheckCredit();
//        int userId = Integer.getInteger((String) request.getAttribute("uID"));
        request.getAttribute("androidUserId");
        int userId = Integer.parseInt(request.getParameter("androidUserId"));
        System.out.println("user id to check out ==================== " + userId);
        String checkCredit = serviceCheckCredit.checkCredit(userId);
        if (checkCredit.equals("buy")) {
            List<Cart> test = serviceCart.buyProductsInCart(userId);
            if (test != null) {
                if (test.size() <= 0) {
                    //save to history and reset cart//////////////////////////////////////
                    response.getWriter().print("done");
                } else //some products are out of stock
                {
                    response.getWriter().print("Sorry some products are out of stock");
                }
            } else {
                response.getWriter().print("Sorry your cart is empty");
            }
        } else // not enough credit
        {
            response.getWriter().print("Sorry you don't have enough credit");
        }
    }
}
